package com.ruoyi.inspection.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.inspection.controller.GovernInspectionTaskPush;
import com.ruoyi.inspection.domain.GovernHiddenInventory;
import com.ruoyi.inspection.exception.ServiceException;
import com.ruoyi.inspection.mapper.GovernHiddenInventoryMapper;
import com.ruoyi.inspection.service.GovernHiddenInventoryService;
import com.ruoyi.inspection.utils.IdFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专项检查隐患信息表(GovernHiddenInventory)表服务实现类
 *
 * @author makejava
 * @since 2024-07-30 11:37:40
 */
@Service("governHiddenInventoryService")
public class GovernHiddenInventoryServiceImpl implements GovernHiddenInventoryService {
    @Resource
    private GovernHiddenInventoryMapper governHiddenInventoryMapper;
    @Resource
    private GovernInspectionTaskPush governInspectionTaskPush;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GovernHiddenInventory queryById(Integer id) {
        return this.governHiddenInventoryMapper.queryById(id);
    }

    /**
     * 查隐患
     * @param governHiddenInventory 筛选条件
     * @return 查询结果
     */
    @Override
    public GovernHiddenInventory queryByInventory(GovernHiddenInventory governHiddenInventory) {
        List<GovernHiddenInventory> list = this.queryByPage(governHiddenInventory);

        return StringUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 分页查询
     *
     * @param governHiddenInventory 筛选条件
     * @return 查询结果
     */
    @Override
    public List<GovernHiddenInventory> queryByPage(GovernHiddenInventory governHiddenInventory) {
        return this.governHiddenInventoryMapper.queryAllByLimit(governHiddenInventory);
    }

    /**
     * 新增数据
     *
     * @param governHiddenInventory 实例对象
     * @return 实例对象
     */
    @Override
    public GovernHiddenInventory insert(GovernHiddenInventory governHiddenInventory) {
        Long taskPlanId = governHiddenInventory.getGovernTaskPlanId();
        String taskItemId = governHiddenInventory.getGovernCheckTaskItemId();
        if (taskPlanId!=null && StringUtils.isNotEmpty(taskItemId)) {
            governHiddenInventoryMapper.deleteByPlanIdAndItemId(taskPlanId, taskItemId);
        }

        governHiddenInventory.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_CONFIRM);
        if (StringUtils.isEmpty(governHiddenInventory.getHiddenBelongType())) {
            governHiddenInventory.setHiddenBelongType(InspectionConstant.YIN_HUAM_LEI_BIE);
        }
        if (StringUtils.isEmpty(governHiddenInventory.getHiddenFrom())) {
            governHiddenInventory.setHiddenFrom(InspectionConstant.JIAN_CHA_LAI_YUAN);
        }
        if (StringUtils.isEmpty(governHiddenInventory.getCheckLevel())) {
            governHiddenInventory.setCheckLevel(InspectionConstant.JIAN_CHA_JI_BIE);
        }
//        governHiddenInventory.setHiddenInventoryId(IdFactory.newLongId());
        governHiddenInventory.setGovernCreateDate(new Date());
        governHiddenInventory.setHiddenCheckTime(new Date());

        this.governHiddenInventoryMapper.insert(governHiddenInventory);
        return governHiddenInventory;
    }

    /**
     * 修改数据
     *
     * @param governHiddenInventory 实例对象
     * @return 实例对象
     */
    @Override
    public GovernHiddenInventory update(GovernHiddenInventory governHiddenInventory) {
        governHiddenInventory.setGovernUpdateDate(new Date());
        if (InspectionConstant.HIDDEN_PROCESS_RECOMMIT.equals(governHiddenInventory.getHiddenProgress())) {
            governHiddenInventory.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_CONFIRM);
        }
        this.governHiddenInventoryMapper.update(governHiddenInventory);
        return this.queryById(governHiddenInventory.getHiddenInventoryId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.governHiddenInventoryMapper.deleteById(id) > 0;
    }

    /**
     * 确认隐患
     *
     * @param governHiddenInventory
     * @param user
     */
    @Override
    public void confirm(GovernHiddenInventory governHiddenInventory, LoginUser user) {
        Integer governConfirm = governHiddenInventory.getGovernConfirm();
        Integer hiddenInventoryId = governHiddenInventory.getHiddenInventoryId();
        if (governConfirm==null || hiddenInventoryId==null) {
            throw new ServiceException("参数传递异常");
        }
        GovernHiddenInventory entity = governHiddenInventoryMapper.queryById(hiddenInventoryId);

        Long userId = user.getUserId();
        Integer rectificationUserId = entity.getRectificationUserId();
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        // 非整改人、非超管、非专项管理员，不能进行隐患确认
        if ( rectificationUserId.intValue() != userId.intValue() && userId != 1 && !zxgly ) {
            throw new ServiceException("您无权操作此条隐患记录");
        }

        // 隐患审核：通过进入整改，不通过直接删除
        if (governConfirm ==1) {
            governHiddenInventory.setHiddenState(1);
            governHiddenInventory.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_REPAIR);

            entity.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_REPAIR);
            entity.setGovernUpdateBy(user.getUsername());
            entity.setGovernUpdateDate(new Date());
            // 隐患上报成功， 修改上报状态
            if (governInspectionTaskPush.pushSpecialCheckInvestigation(entity)) {
                governHiddenInventory.setGovernUpload(1);
            }
        } else {
            governHiddenInventory.setHiddenState(2);
            governHiddenInventory.setGovernDeleted(1);
        }

        governHiddenInventory.setGovernUpdateDate(new Date());
        governHiddenInventory.setGovernUpdateBy(user.getUsername());
        this.governHiddenInventoryMapper.update(governHiddenInventory);
    }

    /**
     * 隐患整改
     *
     * @param governHiddenInventory
     * @param user
     */
    @Override
    public void repair(GovernHiddenInventory governHiddenInventory, LoginUser user) {
        Integer hiddenInventoryId = governHiddenInventory.getHiddenInventoryId();
        if (hiddenInventoryId==null) {
            throw new ServiceException("参数传递异常");
        }
        GovernHiddenInventory entity = governHiddenInventoryMapper.queryById(hiddenInventoryId);

        Long userId = user.getUserId();
        Integer rectificationUserId = entity.getRectificationUserId();
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        // 非整改人、非超管、非专项管理员，不能进行整改
        if (rectificationUserId.intValue() != userId.intValue() && userId != 1 && !zxgly ) {
            throw new ServiceException("您无权操作此条隐患记录",500);
        }
        governHiddenInventory.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_REVIEW);
        governHiddenInventory.setHiddenCompleteTime(new Date());
        governHiddenInventory.setGovernUpdateDate(new Date());
        this.governHiddenInventoryMapper.update(governHiddenInventory);

        entity.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_REVIEW);
        entity.setGovernUpdateBy(user.getUsername());
        entity.setHiddenCompleteTime(new Date());
        entity.setGovernUpdateDate(new Date());
        governInspectionTaskPush.pushSpecialCheckInvestigation(entity);
    }

    @Override
    public void review(GovernHiddenInventory governHiddenInventory, LoginUser user) {
        Integer hiddenInventoryId = governHiddenInventory.getHiddenInventoryId();
        if (hiddenInventoryId==null) {
            throw new ServiceException("参数传递异常");
        }
        GovernHiddenInventory entity = governHiddenInventoryMapper.queryById(hiddenInventoryId);

        Long userId = user.getUserId();
        Integer reviewUserId = entity.getReviewUserId();
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        // 非复核人、非超管、非专项管理员，不能进行复核
        if (reviewUserId.intValue() != userId.intValue() && userId != 1 && !zxgly ) {
            throw new ServiceException("您无权操作此条隐患记录",500);
        }
        governHiddenInventory.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_COMPLETE);
        governHiddenInventory.setHiddenReviewTime(new Date());
        governHiddenInventory.setGovernUpdateDate(new Date());
        governHiddenInventory.setGovernUpdateBy(user.getUsername());
        this.governHiddenInventoryMapper.update(governHiddenInventory);

        entity.setHiddenProgress(InspectionConstant.HIDDEN_PROCESS_COMPLETE);
        entity.setGovernUpdateBy(user.getUsername());
        entity.setHiddenReviewTime(new Date());
        entity.setGovernUpdateDate(new Date());
        governInspectionTaskPush.pushSpecialCheckInvestigation(entity);
    }

    @Override
    public void upload(Long[] hiddenInventoryIds) {
//        GovernHiddenInventory governHiddenInventory = governHiddenInventoryMapper.queryById(id);
    }
}
